//
//  ZZNetworkTool.m
//  ZuZuBao
//
//  Created by Felix on 2024/4/8.
//

#import "ZZNetworkTool.h"

static ZZNetworkTool * globalManager = nil;

@implementation ZZNetworkTool

+ (ZZNetworkTool *)Share
{
    if (!globalManager) {
        globalManager = [[ZZNetworkTool alloc] init];
    }
    return globalManager;
}

- (id)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)networkRequestWithRequestType:(ZZNetRequestType)requestType
                     requestUrlString:(NSString *)urlString
                           parameters:(NSDictionary *)parameters
                         successBlock:(ResultBlock)successBlock
                         failureBlock:(ResultBlock)failureBlock
{
    
    self.requestSerializer = [AFJSONRequestSerializer serializer];
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    self.requestSerializer.timeoutInterval = 10.0;
    
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",
                                                      @"application/json",@"text/javascript",
                                                      @"text/html", @"application/javascript",
                                                      @"application/x-javascript",@"text/js", @"application/x-www-form-urlencoded", nil];
    
    void(^success)(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successBlock(responseObject, nil);
    };
    
    void(^failure)(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlock(nil, error);
        if (error.code == NSURLErrorTimedOut) {
//            [MBProgressHUD :@"请求超时" toView:nil];
        }
    };
    
    switch (requestType) {
        case ZZNetRequestTypePost:
        {
//            NSLog(@"当前请求POST.URL===%@%@", urlString, parameters);
            self.task = [self POST:urlString parameters:parameters headers:nil progress:nil success:success failure:failure];
        }
            break;
        case ZZNetRequestTypeGet:
        {
            //            NSLog(@"当前请求GET.URL===%@", urlString);
            self.task = [self GET:urlString parameters:parameters headers:nil progress:nil success:success failure:failure];
        }
            break;
            
        default:
            break;
    }
}

+(AFHTTPSessionManager *)sharedManager
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //最大请求并发任务数
    manager.operationQueue.maxConcurrentOperationCount = 5;

    // 请求格式
    // AFHTTPRequestSerializer            二进制格式
    // AFJSONRequestSerializer            JSON
    // AFPropertyListRequestSerializer    PList(是一种特殊的XML,解析起来相对容易)
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; // 上传普通格式
    
    // 超时时间
    manager.requestSerializer.timeoutInterval = 30.0f;
    // 设置请求头
//    [manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
    // 设置接收的Content-Type
    
    // 返回格式
    // AFHTTPResponseSerializer           二进制格式
    // AFJSONResponseSerializer           JSON
    // AFXMLParserResponseSerializer      XML,只能返回XMLParser,还需要自己通过代理方法解析
    // AFXMLDocumentResponseSerializer (Mac OS X)
    // AFPropertyListResponseSerializer   PList
    // AFImageResponseSerializer          Image
    // AFCompoundResponseSerializer       组合
    
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//返回格式 JSON
    // 设置接收的Content-Type
    manager.responseSerializer.acceptableContentTypes=[[NSSet alloc] initWithObjects:@"application/xml", @"text/xml",@"text/html", @"application/json", @"text/plain", @"application/*+json", nil];

    return manager;
}


@end
